//
//  ProfitNewsController.m
//  PosApp
//
//  Created by 李英新 on 2018/10/12.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ProfitNewsController.h"
#import "ProfitNewsViewCell.h"
#import "ProfitNewsModel.h"

@interface ProfitNewsController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *NewsTable;
@property (strong, nonatomic) NSMutableArray * cellArr;
@property (strong, nonatomic) MBProgressHUD * hud;
@property (assign, nonatomic) NSUInteger page;

@end

@implementation ProfitNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"收支明细";
    self.NewsTable.delegate = self;
    self.NewsTable.dataSource = self;
    self.NewsTable.rowHeight = 76;
    self.NewsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.NewsTable registerNib:[UINib nibWithNibName:@"ProfitNewsViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.NewsTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.cellArr removeAllObjects];
        [self UpLoadDataOfNews];
    }];
    self.NewsTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page = self.page+1;
        [self UpLoadDataOfNews];
    }];
    [self.NewsTable.mj_header beginRefreshing];
}

- (void)UpLoadDataOfNews{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [dic setObject:self.NewsType forKey:@"type"];
    [dic setObject:[NSString stringWithFormat:@"%ld",self.page] forKey:@"page"];
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"details" andAction:@"Income" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求返回信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                ProfitNewsModel * model = [ProfitNewsModel mj_objectWithKeyValues:dic];
                [self.cellArr addObject:model];
            }
            [self.NewsTable reloadData];
            
        }
        [self.NewsTable.mj_header endRefreshing];
        [self.NewsTable.mj_footer endRefreshing];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
        [self.NewsTable.mj_header endRefreshing];
        [self.NewsTable.mj_footer endRefreshing];
        [self.hud hideAnimated:YES];
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProfitNewsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ProfitNewsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    ProfitNewsModel * model = self.cellArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (NSMutableArray *)cellArr {
    if (!_cellArr) {
        _cellArr = [NSMutableArray array];
    }
    return _cellArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
